package com.fz.controller;

import com.alibaba.fastjson.JSONObject;
import com.fz.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Z-C-Y on 2018-01-31.
 */
@Controller
public class timerController {
   // @RequestMapping("/timer") @ResponseBody
    public String timer(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return sdf.format(new Date());
    }

   // @RequestMapping("/timer")
    public void timer(PrintWriter out){
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        out.print(s.format(new Date()));

    }

    /*使用json进行传值*/
    @RequestMapping("/json") @ResponseBody
    public void stu(PrintWriter out, HttpServletResponse resp){
        resp.setContentType("text/html;charset=utf-8");
        String[] s = new String[]{"静静","濮阳","上海"};
       out.print(JSONObject.toJSON(s));
    }

    /*连接数据库*/
    @RequestMapping("/student") @ResponseBody
    public String student(){
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(is);
        SqlSession s = sf.openSession();

       StudentMapper ss =  s.getMapper(StudentMapper.class);

     return JSONObject.toJSONString(ss.query());

    }

}
